from socket import *
from threading import Thread
import os

class ServeHandle():
    def __init__(self,conn:socket,file_name):
        self.conn = conn
        self.file_name = file_name
    def main(self):
        pass

class Find(ServeHandle):
    def main(self):
        file_name = os.listdir("123")
        send = " ".join(file_name)
        self.conn.send(send.encode())

class Up_Transport(ServeHandle):
    def main(self):
        # 存放地址
        fp = open("*****{}".format(self.file_name),"ab")
        self.conn.send("请开始上传".encode())
        while True:
            get = self.conn.recv(1024)
            if get.decode() == "##00##":
                break
            fp.write(get)
        fp.close()

class MoreThread(Thread):
    def __init__(self,conn):
        self.conn = conn
        super().__init__()

    def run(self):
        while True:
            get = self.conn.recv(1024 * 1024).decode()
            handle = get.split(" ")[0]
            handle = eval(handle)
#            handle.__main__ = Find     函数eval
            do = handle(self.conn,handle[1])
            do.main()

class Dow_Servive():
    def __init__(self,addr,poin):
        self.ADDR = (addr,poin)
        self.sock = self._creat_soct()

    def main(self):
        self.sock.listen()
        while True:
            conn, addr = self.sock.accept()
            print("{}已连接服务器".format(addr))
            th = MoreThread(conn)
            th.start()

    def _creat_soct(self):
        sock = socket()
        sock.bind(self.ADDR)
        return sock

if __name__ == '__main__':
    serve = Dow_Servive("0.0.0.0",8889)
    serve.main()



